package com.example.cloud.controller.basic.info;

import com.example.cloud.controller.common.BaseController;
import com.example.cloud.model.basic.Product;
import com.example.cloud.model.dto.QueryModel;
import com.example.cloud.model.dto.Result;
import com.example.cloud.model.dto.SearchModel;
import com.example.cloud.service.basic.info.ProductService;
import com.example.cloud.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by wangtao on 2018/6/6.
 */
@RestController
@RequestMapping("/product")
public class ProductController extends BaseController {

    @Autowired
    private ProductService productService;

    @PostMapping("/search")
    public Result search(@RequestBody QueryModel model) {
        return this.singleQuery(model, Product.class);
    }

    @PostMapping("/searchByModel/{id}/{pageNumber}/{pageSize}")
    public Result searchByModel(@PathVariable Long id, @PathVariable int pageNumber, @PathVariable int pageSize, @RequestBody List<SearchModel> conditions) {
        return productService.doSearchModel(id, pageNumber, pageSize, conditions);
    }

    @PostMapping("/saveOrUpdate")
    public void saveOrUpdate(@RequestBody Product record) {
        productService.doSave(record);
    }

    @PostMapping("/delete")
    public void delete(@RequestBody List<Long> ids) {
        productService.doDelete(ids);
    }

    @GetMapping("/findAll")
    public Result findAll() {
        return ResultUtil.success(productService.findAllFromCache());
    }

    @GetMapping("/findBySmallClassId/{id}")
    public Result findBySmallClassId(@PathVariable Long id) {
        return ResultUtil.success(productService.findAllBySmallClassIdFromCache(id));
    }
}
